<template>
  <div class="login">
    <el-form ref="userFormRef" :model="userForm" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="userForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="userForm.password" type="password"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" round @click="login">登录</el-button>
  </div>
</template>

<script>
import { login } from '@/api/api'
import { setItem } from '@/utils/storage'
export default {
  name: 'Login',
  data () {
    return {
      userForm: {
        username: 'admin',
        password: 123456
      }
    }
  },
  methods: {
    async login () {
      const { data: res } = await login(this.userForm)
      if (res.code !== 20000) {
        this.$message.error(res.data)
      } else {
        this.$message.success('登录成功！')
        setItem('userInfo', res.data.user)
        setItem('token', res.data.token)
        this.$router.push('/admin/home')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login{
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  width: 400px;
  height: 200px;
  border: 1px solid #f7f7f7;
  background-color: #f7f7f7;
  margin: auto;
}
</style>
